﻿using ConsulDiscovery.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using System.Web.Http;

namespace ConsulDiscovery.Controllers
{
    public class GetRemoteController : ApiController
    {
        public async Task<string> Get()
        {
            var serviceHost = await RoundRobinLoadBalancer.GetInstance().GetServiceAsync();
            if (String.IsNullOrEmpty(serviceHost))
            {
                return "Service Endpoint is empty.";
            }

            var endpoint = "http://" + serviceHost + "/api/user";


            using (var httpClient = HttpClientFactory.Create())
            {
                var response = await httpClient.GetStringAsync(endpoint);

                return "InvokeRemoteAPI, url:" + endpoint + ", response: " + response;
            }

            
        }
    }
}
